package mfuny.ggongal.admin.http;

import mfuny.ggongal.admin.debug.Debug;
import mfuny.ggongal.admin.https.GGDecoder;
import mfuny.ggongal.admin.https.GGEncoder;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class HttpAdDecoder {
	
	@SuppressWarnings("unchecked")
	public String receiveDecode(String enStr) throws Exception {
		String deStr = GGDecoder.decode(enStr);
		
		JSONObject paramJson = (JSONObject)JSONValue.parse(deStr);
		JSONObject resultJson = new JSONObject();
		HttpAd ad = new HttpAd();
		
		//오늘의 꽁게임.
		if( paramJson.get("tag").equals("ad_ggongToday")) {
			try {
				resultJson = ad.ad_ggongToday();
			} catch ( Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//오늘의 경품게임.
		else if( paramJson.get("tag").equals("ad_specialToday")) {
			try {
				resultJson = ad.ad_specialToday();
			} catch ( Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		//오늘의 경품게임. 알람전용.
		else if ( paramJson.get("tag").equals("ad_alarmToday")) {
			try {
				resultJson = ad.ad_alarmToday();
			} catch ( Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//광고번호로 특정광고정보 가져오기.
		else if ( paramJson.get("tag").equals("ad_oneAd")) {
			try {
				resultJson = ad.ad_oneAd(Integer.parseInt((String)paramJson.get("ad_no")));
			} catch ( Exception e ) {
				Debug.toDebugFile(e, paramJson.get("tag") + " / ad_no : "+ paramJson.get("ad_no"));
			}
		}
		
		//광고번호로 특정 광고정보 가져오기(다중).
		else if ( paramJson.get("tag").equals("ad_adsInfo")) {
			try {
				resultJson = ad.ad_adsInfo( (String)paramJson.get("ads"));
			} catch ( Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//광고번호로 해당 경품 정보 가져오기.
		else if ( paramJson.get("tag").equals("ad_getGiftDetail")) {
			try {
				resultJson = ad.ad_getGiftDetail(Integer.parseInt((String)paramJson.get("ad_no")));
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
			}
		}
		
		//광고번호로 해당 경품 정보 가져오기(등수 포함).
		else if ( paramJson.get("tag").equals("ad_getGiftDetail2")) {
			try {
				resultJson = ad.ad_getGiftDetailWithRank(Integer.parseInt((String)paramJson.get("ad_no")));
			} catch ( Exception e) {
				Debug.toDebugFile(e, paramJson.get("tag") + " / " + paramJson.get("ad_no"));
			}
		}
		
		//특정 광고 실시간 랭킹.
		else if ( paramJson.get("tag").equals("ad_getRank")) {
			try {
				resultJson = ad.ad_getRank(
						(String)paramJson.get("id"),
						Integer.parseInt((String)paramJson.get("ad_no")), 
						Integer.parseInt((String)paramJson.get("start")),
						(String)paramJson.get("days")
				);
			} catch (Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
				resultJson.put("error", e);
			}
		}
		
		//전일 당첨자 리스트.
		else if ( paramJson.get("tag").equals("ad_getYesterdayWinner")) {
			try {
				resultJson = ad.ad_getYesterdayWinner();
			} catch ( Exception e) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -2);
				resultJson.put("error", e);
			}
		}
		
		//스페셜 게임에 해당 휴대폰번호로 플레이 한 경우 진입 제한.
		else if ( paramJson.get("tag").equals("ad_checkSGame")) {
			try {
				resultJson = ad.ad_checkSGame(
						(String)paramJson.get("id"),
						Integer.parseInt((String)paramJson.get("ad_no")), 
						Integer.parseInt((String)paramJson.get("hour"))
				);
				
			} catch (Exception e ) {
				Debug.toDebugFile(e, (String)paramJson.get("tag"));
				resultJson.put("responseCode", -1);
			}
		}
		
		// 암호화 되어있으나 tag가 없을때.
		else {
			Debug.toDebugFile("tag 없음 - " + (String) paramJson.get("tag"));
		}
		
		return GGEncoder.encodeForJson(resultJson);
	}
}




























